﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AddIn.EA.Architecture.ComponentLogic.EAConverter
{
    public class EAConverterAttribute : EAConverter
    {
        public override Framework.Common.IElement ConvertObject(ElementModel.BaseModel sourceElement, Framework.Common.IElement affectedElement)
        {
            //Validate element conversion types.
            if (!(sourceElement is ElementModel.Attribute))
                throw new InvalidOperationException("Invalid type. The source parameter \"sourceElement\" must be \"ElementModel.Attribute\" type.");
            if (!(affectedElement is Framework.Element) && !(affectedElement is Framework.Element))
                throw new InvalidOperationException("Invalid type. The source parameter \"affectedElement\" must be \"AddIn.EA.Framework.Element\" type.");

            //Create the typed local elements.
            ElementModel.Attribute attribute = sourceElement as ElementModel.Attribute;
            Framework.Element destinationElement = affectedElement as Framework.Element;

            Framework.AttributeClass attributeEA = destinationElement.Attributes.AddNew(attribute.Name);
            attributeEA.Alias = attribute.Alias;
            attributeEA.InitialValue = attribute.InitialValue;
            attributeEA.IsConst = attribute.IsConst;
            attributeEA.IsStatic = attribute.IsStatic;
            attributeEA.Name = attribute.Name;
            attributeEA.Notes = attribute.Notes;
            attributeEA.Scope = (AddIn.EA.Framework.ElementScope)Enum.Parse(typeof(Framework.ElementScope), attribute.Scope.ToString());
            attributeEA.Stereotype = attribute.Stereotype;
            attributeEA.Type = attribute.Type;
            if (attribute.Encapsulate)
                attributeEA.CreateProperty();

            BindAttributeTaggedValues(attributeEA, attribute.TaggedValues);

            attributeEA.Update();
            destinationElement.Update();

            return attributeEA;
        }
    }
}
